package com.wingstudio.springcloud.controller;

import com.wingstudio.springcloud.redis.RedisService;
import com.wingstudio.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @Author ITcz
 * @Data 2021-04-15 - 16:36
 */
@RestController
@RequestMapping("/payment/hystrix")
@Slf4j
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @Autowired
    RedisService redisService;

    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/lock/{id}")
    public String paymentInfo_Lock(@PathVariable("id") Integer id) {
//        String result = paymentService.paymentInfo_OK(id);
        String requestId = UUID.randomUUID().toString();
        String lockId = id.toString();
        boolean result = redisService.lock("paymentInfo", lockId, requestId);
        log.info("**********result" +result);
        return result + "线程池: " + Thread.currentThread().getName()+" paymentInfo_LOCK, id: " + id;
    }

    @GetMapping("/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        String result = paymentService.paymentInfo_OK(id);
        log.info("**********result" +result);
        return result;
    }


    @GetMapping("/error/{id}")
    public String paymentInfo_ERROR(@PathVariable("id") Integer id) {
        String result = paymentService.paymentInfo_ERROR(id);
        log.info("**********result" +result);
        return result;
    }

    @GetMapping("/circuit/{id}")
    public String paymentCircuitBreaker(@PathVariable("id") Integer id) {
        String s = paymentService.paymentCircuitBreaker(id);
        log.info("***********result" + s);
        return s;
    }


}
